﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLK.Database;
using QLK.Database.BLLs;
using QLK.Web.App_Code;

namespace QLK.Web
{
    public partial class ExportInvoices : System.Web.UI.Page
    {
        private string _CustID;
        private string _InvoiceID;
        private ExportInvoiceBLL ExpBLL = new ExportInvoiceBLL();
        private StorageBLL StorageBLL = new StorageBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            _CustID = Request.QueryString[Columns.CustomerID];
            _InvoiceID = Request.QueryString[Columns.InvoiceID];
            if (!IsPostBack)
                Load_Data();            
        }

        private void Load_Data()
        {                   
            //Load drlStorages
            drlStorages.DataSource = StorageBLL.StorageGetAll();
            drlStorages.DataTextField = Columns.StorageName;
            drlStorages.DataValueField = Columns.StorageID;

            if (string.IsNullOrEmpty(_InvoiceID))
            {
                txtInvoiceID.Text = null;
            }
            else
            {
                ExportInvoice expinvoice = ExpBLL.ExportInvoiceGetByInvoiceID(_InvoiceID);
                txtInvoiceID.Text = expinvoice.InvoceID;
                txtCustID.Text = expinvoice.CustomerID.ToString();
                txtEmpID.Text = expinvoice.EmployeeID.ToString();
                dpExportDate.SelectedDate = expinvoice.ExportDate;
                drlStorages.SelectedValue = expinvoice.StorageID.ToString();
            }

            if (string.IsNullOrEmpty(_CustID))
            {
                txtCustID.Text = null;
                ImgSeach.Enabled = true;
            }
            else
            {
                txtCustID.Text = _CustID.ToString();
                ImgSeach.Enabled = false;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_InvoiceID))
            {
                ExportInvoice exportinvoice = new ExportInvoice();
                exportinvoice.InvoceID = txtInvoiceID.Text;
                exportinvoice.StorageID = int.Parse(drlStorages.SelectedValue);
                exportinvoice.ExportDate = dpExportDate.SelectedDate;
                exportinvoice.CustomerID = int.Parse(txtCustID.Text);
                exportinvoice.EmployeeID = int.Parse(txtEmpID.Text);

                if (ExpBLL.ExportInvoiceInsert(exportinvoice))
                    lblWarning.Text = "Insert export invoice successfully!";
                else
                    lblWarning.Text = "Insert failed!";
            }
            else
            {
                ExportInvoice exp = ExpBLL.ExportInvoiceGetByInvoiceID(_InvoiceID);
                ExportInvoice exportinvoice = new ExportInvoice();
                exportinvoice.ExportID = exp.ExportID;
                exportinvoice.InvoceID = txtInvoiceID.Text;
                exportinvoice.StorageID = int.Parse(drlStorages.SelectedValue);
                exportinvoice.ExportDate = dpExportDate.SelectedDate;
                exportinvoice.CustomerID = int.Parse(txtCustID.Text);
                exportinvoice.EmployeeID = int.Parse(txtEmpID.Text);

                if (ExpBLL.ExportInvoiceUpdate(exportinvoice))
                    lblWarning.Text = "Update successfully";
                else
                    lblWarning.Text = "Update failed!";
            }
        }

        protected void btnSaveAndView_Click(object sender, EventArgs e)
        {

            Response.Redirect(string.Format("{0}?{1}={2}&{3}={4}", PageUrls.ExportInvoiceProducts,
                Columns.ExportID, txtInvoiceID.Text, Columns.CustomerID, txtCustID.Text));
        }
    }
}
